package com.lingnuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lingnuo.pojo.PageRequest;
import com.lingnuo.pojo.User;
import com.lingnuo.service.UserService;
import com.lingnuo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    // 自动注入UserService
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param user 用户
     * @return {@code Map<String, Object> }
     */
    @PostMapping("/login")
    public Result userLogin(@RequestBody User user) { // 使用统一响应对象
        try {
            User result = userService.login(user);
            return result != null ?
                    Result.success("登陆成功") :
                    Result.error("用户名或密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("登录失败: " + e.getMessage());
        }
    }

    /**
     * 用户注册
     *
     * @param user 用户
     * @return {@code Result}
     */
    @PostMapping("/register")
    public Result userRegister(@RequestBody User user) {
        try {
            int usernameResult = userService.selectUserByUsername(user.getUsername());
            if (usernameResult > 0) {
                return Result.error("用户名已存在");
            } else {
                int result = userService.register(user);
                if (result > 0) {
                    return Result.success("注册成功");
                } else {
                    return Result.error("注册失败");
                }
            }
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return Result.error("注册过程中出现错误，请稍后重试: ");
        }
    }

    // 查询所有用户
    @GetMapping("/all")
    public Result getAllUsers(@RequestBody PageRequest pageRequest) {
        try {
            PageInfo<User> pageInfo = userService.getAllUsers(pageRequest);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询用户失败: " + e.getMessage());
        }
    }

    // 根据id查询用户
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable int id) {
        try {
            User result = userService.getUserById(id);
            return result != null ?
                    Result.success(result) :
                    Result.error("用户不存在");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询用户失败: " + e.getMessage());
        }
    }

    // 更新用户
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        try {
            int result = userService.updateUser(user);
            return result > 0 ?
                    Result.success("更新成功") :
                    Result.error("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新用户失败: " + e.getMessage());
        }
    }

    // 根据id删除用户
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable int id) {
        try {
            int result = userService.deleteUser(id);
            return result > 0 ?
                    Result.success("删除成功") :
                    Result.error("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除用户失败: " + e.getMessage());
        }
    }


    /**
     * 上传图片
     *
     * @param image  图像
     * @param userId 用户 ID
     * @return {@code Result }
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("image") MultipartFile image,
                              @RequestParam("userId") int userId) {
        return userService.uploadUserImage(image, userId);
    }
}